/**
 * @Author: easten
 * @Created: 2024-04-15 23:48:14
 * @Description:
 *
 */
import { ApiFacility } from "_common/api/iot/ApiFacility";
import { ApiDict } from "_common/api/sys";
import { DrawerProps, TreeDataModel } from "_common/DataType";
import { IotFacility, SysDict } from "_common/entity";
import { AxiosInstance } from "axios";
import React, { useState, useEffect, useRef } from "react";
import {
  Button,
  Drawer,
  Form,
  FormInstanceFunctions,
  Input,
} from "tdesign-react";
import FormItem from "tdesign-react/es/form/FormItem";
type WorkshopDrawerProps = DrawerProps & {
  station?: TreeDataModel;
  axios: AxiosInstance;
};
const FacilityDrawer: React.FC<WorkshopDrawerProps> = (
  props: WorkshopDrawerProps
) => {
  const { open, axios, edit, onSuccess, value, station } = props;
  const formRef = useRef<FormInstanceFunctions>();

  // 测点类型
  const [typeDict, setTypeDict] = useState<SysDict[]>([]);

  const apiFacility = new ApiFacility(axios);
  const apiDict = new ApiDict(axios);

  useEffect(() => {
    return () => {
      formRef.current?.reset();
    };
  }, []);

  useEffect(() => {
    getDict();
    if (value && edit) {
      formRef.current?.setFieldsValue(value);
    }
  }, [edit, value]);

  const handleClose = () => {
    onSuccess?.(false);
    formRef.current?.reset();
  };

  // 新增
  const createWorkshop = async (data: IotFacility) => {
    const response = await apiFacility.create({
      ...data,
      stationId: station?.value,
    });
    if (response && response.code == 200) {
      handleClose();
    }
  };
  // 更新
  const updateWorkshop = async (data: IotFacility) => {
    const response = await apiFacility.update({
      ...data,
      id: value?.id,
      stationId: station?.value,
    });
    if (response && response.code == 200) {
      handleClose();
    }
  };

  // 获取数据字典
  const getDict = async () => {
    const response = await apiDict.getChildren("Facility");
    if (response && response.code == 200 && response.data) {
      setTypeDict(response.data);
    }
  };

  return (
    <Drawer
      header={`${edit ? "编辑" : "添加"}设施`}
      footer={false}
      visible={open}
      sizeDraggable
      size="medium"
      onClose={handleClose}
    >
      <Form
        ref={formRef}
        labelWidth={100}
        onSubmit={async (data) => {
          // 是否校验通过
          if ((await formRef.current?.validateOnly()) == true) {
            edit
              ? updateWorkshop(data.fields as IotFacility)
              : createWorkshop(data.fields as IotFacility);
          }
        }}
      >
        <FormItem label="设施名称" rules={[{ required: true }]} name="name">
          <Input />
        </FormItem>
        <FormItem label="设施编码" rules={[{ required: true }]} name="code">
          <Input />
        </FormItem>
        {/* <FormItem label="设施类型" rules={[{ required: true }]} name="dicType">
          <Select
            options={typeDict?.map((item) => {
              return { label: item.dictLabel, value: item.dictCode };
            })}
          />
        </FormItem> */}
        <FormItem label="设施描述" rules={[{ required: false }]} name="desc">
          <Input />
        </FormItem>
        <FormItem className="float-right">
          <Button type="submit" theme="primary">
            提交
          </Button>
          <Button type="reset" theme="success" style={{ marginLeft: 12 }}>
            重置
          </Button>
        </FormItem>
      </Form>
    </Drawer>
  );
};
export default FacilityDrawer;
